//
//  WHNHistoryViewController.m
//  YiQiTingShu
//
//  Created by dllo on 15/11/13.
//  Copyright © 2015年 直到世界的尽头. All rights reserved.
//

#import "WHNHistoryViewController.h"
#import "WHNTableViewCell.h"
#import "CoreDataManager.h"
#import "PlayerButton.h"
#import <MBProgressHUD.h>
#import <UIImageView+WebCache.h>
#import "History.h"
#import "FPlayViewController.h"
#import "WHNThirdViewController.h"
#import "PlayHistory.h"

@interface WHNHistoryViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong)UITableView *tableView;

@property(nonatomic, strong)MBProgressHUD *progressView;
@property(nonatomic, strong)CoreDataManager *manager;
@property(nonatomic, strong)UILabel *errorLabel;

@end

@implementation WHNHistoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _dataSource = [NSArray array];
    _manager = [CoreDataManager shareManager];
    [self.view addSubview:self.tableView];
    self.title = @"播放历史";
    if (_historyKind == 1) {
        [self playHistory];
    }
    else{
        [self selectAction];
    }
    
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.view NightWithType:UIViewColorTypeNormal];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setTitle:@"清空历史记录" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    button.frame = FitRectMake(50, 50, 375, 40);
    self.tableView.tableHeaderView = button;
}

-(UILabel *)errorLabel
{
    if (!_errorLabel) {
        _errorLabel = [[UILabel alloc] initWithFrame:FitRectMake(50, 50, 375, 40)];
        [_errorLabel NightWithType:UIViewColorTypeNormal];
        _errorLabel.text = @"没有历史记录";
    }
    return _errorLabel;
}

-(void)buttonAction
{
    if (_historyKind == 1) {
        [_manager deleteAllPlayHistory];
    }
    else{
        [_manager deleteAllHistory];
    }
    _dataSource = nil;
    [self.tableView reloadData];
}

-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [_tableView NightWithType:UIViewColorTypeNormal];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView NightWithType:UIViewColorTypeNormal];
    }
    return _tableView;
}
-(MBProgressHUD *)progressView
{
    if (!_progressView) {
        _progressView = [[MBProgressHUD alloc]initWithView:self.view];
        _progressView.labelText = @"加载中";
        [_progressView setMode:MBProgressHUDModeIndeterminate];
        [_progressView show:YES];
        [_progressView NightWithType:UIViewColorTypeNormal];
    }
    return _progressView;
}

#pragma mark 视图将出现时,上下隐藏
-(void)viewWillAppear:(BOOL)animated
{
//    [PlayerButton shareButton].hidden = YES;
//    self.tabBarController.tabBar.hidden = YES;
//    self.navigationController.navigationBar.translucent = NO;
//    
    
}
#pragma mark 视图将消失时,上下出现
-(void)viewWillDisappear:(BOOL)animated
{
//    [PlayerButton shareButton].hidden = NO;
//    self.tabBarController.tabBar.hidden = NO;
//    self.navigationController.navigationBar.translucent = YES;
//    self.navigationController.navigationBar.barTintColor = [UIColor clearColor];
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"fy_backView"] forBarMetrics:UIBarMetricsDefault];
//    self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
}
#pragma mark -查找数据库里所有历史
#pragma mark 电台历史
-(void)selectAction
{
    //创建查询请求对象
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"History"];
    //查询结果排序方式
    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES];
    [fetchRequest setSortDescriptors:@[sortDescriptor]];
    //将请求对象发给manager
    _dataSource = [_manager.managedObjectContext executeFetchRequest:fetchRequest error:nil];
}

#pragma mark 播放历史
-(void)playHistory
{
    //创建查询请求对象
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"PlayHistory"];
    //将请求对象发给manager
    _dataSource = [_manager.managedObjectContext executeFetchRequest:fetchRequest error:nil];
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *mark = @"cell";
    WHNTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:mark];
    
    if (!cell) {
        cell = [[WHNTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:mark];
        cell.kind = WHNTableViewCellKindSix;
    }
    [cell NightWithType:UIViewColorTypeNormal];
    if (_dataSource.count != 0) {
        History *history = _dataSource [indexPath.row];
        [cell.MainImageView sd_setImageWithURL:[NSURL URLWithString: history.imageUrl] placeholderImage:[UIImage imageNamed:@"placeholderZh"]];
        cell.TitleLabel.text = history.title;
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_dataSource.count == 0) {
        [self.view addSubview:self.errorLabel];
        return 0;
    }
    return _dataSource.count;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_historyKind == 1) {
        FPlayViewController *vc = (FPlayViewController *)_supVC;
        PlayHistory *model = _dataSource[indexPath.row];
        vc.trackID = model.trackID;
        vc.trackUid = model.trackUid;
        vc.albumId = model.albumId;
        [self.navigationController popViewControllerAnimated:YES];
    }
    else{
        WHNThirdViewController *vc = (WHNThirdViewController *)_supVC;
        History *model = _dataSource[indexPath.row];
        vc.WHNPic = model.imageUrl;
        vc.WHNCount = model.acount;
        vc.RadioId = model.radioId;
        vc.Rname = model.rname;
        [self.navigationController popViewControllerAnimated:YES];
    }
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
